package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class LoanContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanContainer
{
    private ArrayList<Loan> loans;
    public static LoanContainer instance;

    /**
     * Constructor for objects of class LoanContainer
     */
    public LoanContainer()
    {
        loans = new ArrayList<Loan>();
    }

    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
        return instance;
    }

    public Loan findLoan(int id)
    {
        Loan desiredLoan = null;
        for(int i=0; i<loans.size(); i++)
        {
            if(loans.get(i).getID() == id)
            {
                desiredLoan = loans.get(i);
            }
        }
        return desiredLoan;    
    }

    public boolean addLoan(Loan l)
    {
        boolean exist=false;
        for(int i=0; i<loans.size(); i++)
        {
            if(loans.get(i).getID() == l.getID())
            {
                exist=true;
            }
        }
        if(!exist)
        {
            loans.add(l);
        }

        return !exist; //the result shows if such loan DOES NOT EXIST
    }

    public void deleteLoan(Loan l)
    {
        if(loans.contains(l))
        {
            loans.remove(l);
        }
    }
}
